package com.example.delicioushunter.view.adapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.example.delicioushunter.R;
import com.example.delicioushunter.model.object.Book;

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class BookAdapter extends ArrayAdapter<Book> {

	private ArrayList<Book> books;
	private Context context;

	public BookAdapter(Context context, ArrayList<Book> books) {
		super(context, R.layout.row_book, books);
		this.context = context;
		this.books = books;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View rowView = inflater.inflate(R.layout.row_book, parent, false);
		RelativeLayout layoutBook = (RelativeLayout) rowView.findViewById(R.id.layoutBook);
		ImageView imgBook = (ImageView) rowView.findViewById(R.id.imgBook);
		TextView txtTitle = (TextView) rowView.findViewById(R.id.txtTitle);

		Book book = books.get(position);
		if (book.isEmpty())
		{
			layoutBook.setBackgroundResource(R.drawable.shelf_blank);
			imgBook.setImageBitmap(null);
			imgBook.setBackgroundColor(0);
			txtTitle.setText(null);
		}
		else {
			Random r = new Random();
			int number = r.nextInt(3) + 1;
			if (number == 1)
				layoutBook.setBackgroundResource(R.drawable.shelf_blue);
			else if (number == 2)
				layoutBook.setBackgroundResource(R.drawable.shelf_brown);
			else if (number == 3)
				layoutBook.setBackgroundResource(R.drawable.shelf_gray);
			
			imgBook.setImageBitmap(book.getImage());
			txtTitle.setText(book.getBook_title());
		}
		return rowView;
	}

}
